package cn.wzg.client.Client.impl;

import cn.wzg.client.Client.RPCClient;
import cn.wzg.entity.Request;
import cn.wzg.entity.Response;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class BIOClient implements RPCClient {
    String host;
    int port;

    public BIOClient(String host, int port) {
        this.host = host;
        this.port = port;
    }

    /***
     * 解耦合，负责客户端的信息发送与接收
     * @param request 请求体(接口名，方法名，参数，参数类型）使用反射
     * @return
     */
    @Override
    public Response sendRequest(Request request) throws IOException, ClassNotFoundException {
        Socket socket = new Socket(host,port);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
        ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());

        System.out.println(request);
        objectOutputStream.writeObject(request);
        objectOutputStream.flush();

        Response response = (Response) objectInputStream.readObject();
        return response;
    }
}
